package datastructure.stack;

import java.util.Stack;

/**
 * Sort emelment in a stack in ascending order with an
 * additional stack  
 * 
 * @author Lei
 */
public class SortStack {

    public static Stack<Integer> sortStack(Stack<Integer> s) {
        Stack<Integer> ret = new Stack<Integer>();
        while (!s.isEmpty()) {
            int sValue = s.pop();
            while (!ret.isEmpty() && ret.peek().intValue() > sValue) {
                s.push(ret.pop());
            }

            ret.push(sValue);
        }
        return ret;
    }

    public static void main(String[] args) {

        Stack<Integer> input = new Stack<Integer>();
        input.push(4);
        input.push(7);
        input.push(1);

        Stack<Integer> ret = SortStack.sortStack(input);
        while (!ret.isEmpty()) {
            System.out.println(ret.pop());
        }


    }
}
